export const cacheMixin = {
  methods: {
    clearCache(str) {
      // keep alive的缓存列表
      const cache = this.$vnode.parent.componentInstance.cache
      // 缓存的key列表
      const keys = this.$vnode.parent.componentInstance.keys
      // 当前传入值的key
      let cacheKey = null
      let cacheIndex = null
      keys.forEach((item, index) => {
        if (item.indexOf(str) !== -1) {
          cacheKey = item
          cacheIndex = index
        }
      })
      if (cacheKey) {
        // 删除缓存列表里的该组件缓存
        keys.splice(cacheIndex, 1)
        delete cache[cacheKey]
      }
    },
    // 关闭当前tag并删除该组件缓存
    closeTag(url, query) {
      this.$store.dispatch('tags/DEL_OWER').then(() => {
        this.clearSelf()
        this.$router.push({
          path: url,
          query: query || {}
        })
      })
    },
    clearSelf() {
      // keep alive的缓存列表
      const cache = this.$vnode.parent.componentInstance.cache
      // 当前组件的key值
      const key = this.$vnode.key == null
        ? this.$vnode.componentOptions.Ctor.cid + (this.$vnode.componentOptions.tag ? `::${this.$vnode.componentOptions.tag}` : '')
        : this.$vnode.key
      // 缓存的key列表
      const keys = this.$vnode.parent.componentInstance.keys
      // 当前传入值的key
      if (cache[key]) {
        if (keys.length) {
          const index = keys.indexOf(key)
          if (index > -1) {
            keys.splice(index, 1)
          }
        }
        delete cache[key]
      }
      this.$destroy()
    }
  }
}
